package name.jiaosha.air.install.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.install.domain.Install;
import name.jiaosha.air.install.domain.InstallItem;
import name.jiaosha.air.install.domain.VisitMessage;
import name.jiaosha.air.install.domain.WorkOrder;
import name.jiaosha.air.install.service.IInstallService;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.AliUtil;
import com.ali8ba.common.PageInfo;

/**
 * 安装单控制器
 * 
 * @author xu
 * 
 */
@Controller
@RequestMapping("/admin/fix")
public class FixController {

	@Autowired
	private IInstallService installService;

	@RequestMapping("/newAdd")
	public ModelAndView toAdd(@RequestBody String jsonData) {

		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> data;

		try {
			data = mapper.readValue(jsonData, Map.class);
			byte[] b = mapper.writeValueAsBytes(data.get("install"));
			Install install = mapper.readValue(b, 0, b.length, Install.class);

			install.setCreateMan("徐声耀");
			System.out.println(install);

			installService.createInstall(install);

		} catch (JsonParseException e) {

			e.printStackTrace();
		} catch (JsonMappingException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", true);
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/findList")
	public ModelAndView getMyFixList(@RequestParam int state) {

		PageInfo<Install> pageInfo = null;

		if (!AppConstants.DEBUG) {
			pageInfo = new PageInfo<Install>();
			List<Install> list = new ArrayList<Install>();
			for (int i = 1000; i < 1020; i++) {
				Install install = new Install();
				install.setDocID(String.valueOf(i));
				install.setPartnerId(i);
				install.setBuyer("张先生" + Math.random());
				install.setPhone("1882566002");
				install.setMobile("8848520");
				install.setAddress("杭州天堂软件园");
				install.setRemarks("备注" + i);
				install.setCreateMan("黄光裕");
				install.setBuyDate(AliUtil.getDaysAfter(-3));
				install.setDeliverDate(AliUtil.getDaysAfter(-2));
				install.setFixDate(new Date());
				if (i == 1002) {
					install.setFixDate(AliUtil.getDaysAfter(-2));
				}
				if (i > 1010) {
					install.setFixDate(AliUtil.getDaysAfter(1));
				}
				list.add(install);
			}

			pageInfo.setItemList(list);

		} else {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("state", state);
			pageInfo = installService.getInstallListByComId(AppConstants.comId,
					1, AppConstants.DEFAULT_SIZE, params);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/fixInfo")
	public ModelAndView fixInfo() {

		Install install = null;
		List<InstallItem> listInstallItem = null;
		List<WorkOrder> listWorkOrder = null;
		List<VisitMessage> listVisitMessage = null;
		if (AppConstants.DEBUG) {
			install = new Install();
			install.setDocID("1000");
			install.setFixDate(new Date());
			install.setPartnerId(2);
			install.setAddress("杭州天堂");
			install.setPhone("05718568");
			install.setMobile("137774363");
			install.setRemarks("要求上午安装");

			listInstallItem = new ArrayList<InstallItem>();
			InstallItem installItem = new InstallItem();
			installItem.setAmount(2);
			installItem.setDocID("1000");
			installItem.setInbarcode("94894033010");
			installItem.setName("海尔小神童");
			installItem.setPower("1.5P");
			installItem.setServerType("1");
			installItem.setOutbarcode("366666666");
			listInstallItem.add(installItem);

			listWorkOrder = new ArrayList<WorkOrder>();
			WorkOrder workOrder = new WorkOrder();

			listWorkOrder.add(workOrder);

			listVisitMessage = new ArrayList<VisitMessage>();

			VisitMessage message = new VisitMessage();
			message.setCategory("A");
			message.setContent("用户满意");
			message.setCreateTime(new Date());
			message.setVisitor("张学友");
			listVisitMessage.add(message);
		} else {

		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", install);
		model.addAttribute("installItem", listInstallItem);
		model.addAttribute("workOrders", listWorkOrder);
		model.addAttribute("visitMessages", listVisitMessage);
		return new ModelAndView("jsonView", model);
	}

}
